﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Buddy.CommonBot;


namespace WingIt
{
    [RevisionControlId("$Id: NamedThrottle.cs 684 2012-08-08 10:09:07Z Chinajade $")]
    public class Throttle_Timed : Throttle
    {
        public Throttle_Timed(string name)
            : base(name)
        {
            // empty
        }


        /// <summary>
        /// <para>Returns true, if the throttle identified by THROTTLENAME is ready to be used.</para>
        /// <para>Notes:<list type="bullet">
        /// <item><description><para> * If the throttle has never been used (e.g., first time), then it is deemed ready (true is returned).</para></description></item>
        /// <item><description><para> * A throttle is 'not ready' until its cooldown time has elapsed since the last call to <c>NamedThrottle.Used()</c>.</para></description></item>
        /// </list></para>
        /// </summary>
        /// <param name="ability"></param>
        /// <returns></returns>
        ///  7Jul2012-21:11UTC chinajade
        public bool  IsReady()
        {
            return ((DateTime.Now - _lastUsed) > _cooldownDuration);
        }


        /// <summary>
        /// <para>Marks the throttle identifid by THROTTLENAME as 'not ready', until the COOLDOWNTIME expires.</para>
        /// <para>Notes:<list type="bullet">
        /// <item><description><para> * If the throttle has never been used (e.g., first time), this method automatically creates it.</para></description></item>
        /// <item><description><para> * Once this method is called, a throttle is 'not ready' until its cooldown time has elapsed.</para></description></item>
        /// </list></para>
        /// </summary>
        /// <param name="ability"></param>
        ///  7Jul2012-21:11UTC chinajade
        public void Used(TimeSpan cooldownDuration)
        {
            Logger.ContractRequires(() => cooldownDuration >= TimeSpan.Zero,
                                    () => string.Format("cooldownTime >= TimeSpan.Zero (cooldownTime: {0})", cooldownDuration));

            _cooldownDuration = cooldownDuration;
            _lastUsed = DateTime.Now;
        }


        /// <summary>
        /// <para>Similar to ToString(), but takes the THROTTLENAME as argument.</para>
        /// </summary>
        /// <param name="throttleName"></param>
        /// <returns></returns>
        /// 16Jul2012-18:46UTC chinajade
        // NB: We intentionally named it AsString() to avoid any implication that we are overriding the ToString() method.
        public override string ToString()
        {
            return (string.Format("NamedThrottle('{0}',  IsReady: {1}", _name, (IsReady() ? "Ready" : "Throttled")));
        }

            
        // Private data
        private TimeSpan _cooldownDuration = TimeSpan.Zero;
        private DateTime _lastUsed = DateTime.Now;
    }
}
